package leetcode.code;

import java.util.ArrayList;
import java.util.List;

/**
 * @author : zx
 * @version V1.0
 */
public class easy_设计Hash集合 {

    class MyHashSet {

        List[] vals;
        /** Initialize your data structure here. */
        public MyHashSet() {
            vals = new List[1000];
        }

        public void add(int key) {
            if (this.contains(key)) return;
            int tmp = key % 1000;
            if (vals[tmp] == null){
                List a = new ArrayList();
                a.add(key);
                vals[tmp] = a;
            }else {
                List a = vals[tmp];
                a.add(key);
            }
        }

        public void remove(int key) {
            int tmp = key % 1000;
            if(vals[tmp] != null){
                List a = vals[tmp];
                for(int i = 0; i < a.size(); i++){
                    if(a.get(i).equals(key)){
                        a.remove(i);
                        break;
                    }
                }
            }
        }

        /** Returns true if this set contains the specified element */
        public boolean contains(int key) {
            int tmp = key % 1000;
            if(vals[tmp] != null){
                List a = vals[tmp];
                for(int i = 0; i < a.size(); i++){
                    if(a.get(i).equals(key)){
                        return true;
                    }
                }
            }
            return false;
        }
    }

/**
 * Your MyHashSet object will be instantiated and called as such:
 * MyHashSet obj = new MyHashSet();
 * obj.add(key);
 * obj.remove(key);
 * boolean param_3 = obj.contains(key);
 */
}
